<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:insert="~{include :: header('新增帮助菜单')}" />
    <th:block th:insert="~{include :: datetimepicker-css}" />
    <th:block th:insert="~{include :: summernote-css}" />
    <style>
        .form-radio {
            margin-right: 5px;
            padding: 5px 5px 5px 0;
        }
        .form-radio span{
            display: inline-block;
            margin-left: 2px;
            vertical-align: 2px;
        }
    </style>
</head>
<body class="hold-transition">
<div class="container-fluid mb-2">
        <form class="form-horizontal m" id="form-helpmenu-add">
            <div class="form-group row" style="margin-top: 30px">
                <label class="col-sm-2 col-form-label text-sm-right">级别</label>
                <div class="col-sm-6">
                    <label class="form-radio">
                        <input type="radio" checked="checked" value="grade1" name="classify">
                        <span>一级菜单</span>
                    </label>
                    <label class="form-radio">
                        <input type="radio"  value="choice"  name="classify">
                        <span>二级菜单</span>
                    </label>
                </div>
            </div>
            <div class="form-group row" id="parentId" style="display: none">
                <label class="col-sm-2 col-form-label text-sm-right">分类</label>
                <div class="col-sm-3">
                    <div class="input-group">
                        <input id="treeId" name="parentId" type="hidden" th:value="0"/>
                        <input class="form-control" type="text" onclick="selectHelpmenuTree()" placeholder="请选择" id="treeName" readonly="readonly" th:value="${helpMenu?.name}">
                        <span class="input-group-addon"><i class="fa fa-search"></i></span>
                    </div>
                </div>
            </div>
            <div class="form-group row">
                <label class="col-sm-2 col-form-label text-sm-right">名称</label>
                <div class="col-sm-3">
                    <input name="name" class="form-control" placeholder="请输入菜单名称" type="text">
                </div>
            </div>
            <div class="form-group row">
                <label class="col-sm-2 col-form-label text-sm-right">排序</label>
                <div class="col-sm-2">
                    <input name="sort" class="form-control" th:value="0" type="text">
                </div>
                <div style="color: red;font-size: 12px;line-height: 34px">*倒序!</div>
            </div>
            <div class="form-group row">
                <label class="col-sm-2 col-form-label text-sm-right">外链</label>
                <div class="col-sm-2">
                    <input  name="linkBuilding" class="form-control" type="text">
                </div>
                <div style="color: red;font-size: 12px;line-height: 34px">*(选填)外链为空则显示帮助内容，不为空则跳转到外链!</div>
            </div>
            <div class="form-group row">
                <label class="col-sm-2 col-form-label text-sm-right">内容</label>
                <div class="col-sm-8">
                    <input id="noticeContent" name="content" type="hidden">
                    <div id="editor" class="summernote"></div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:insert="~{include :: footer}" />
    <th:block th:insert="~{include :: datetimepicker-js}" />
    <th:block th:insert="~{include :: summernote-js}" />
    <script type="text/javascript">
        var prefix = "/help"
        $("#form-helpmenu-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            var sHTML = $('.summernote').summernote('code');
            $("#noticeContent").val(sHTML);
            if ($.validate.form()) {
                $.operate.save(prefix + "/add.json", $('#form-helpmenu-add').serialize());
            }
        }

        /*帮助菜单-新增-选择父部门树*/
        function selectHelpmenuTree() {
            var options = {
                title: '帮助菜单选择',
                width: "380px",
                height: "380px",
                url: prefix + "/tree/" + $("#treeId").val(),
                callBack: doSubmit
            };
            $.modal.openOptions(options);
        }

        function doSubmit(index, layero){
            var body = layer.getChildFrame('body', index);
               $("#treeId").val(body.find('#treeId').val());
               $("#treeName").val(body.find('#treeName').val());
               layer.close(index);
        }

        //次数限制事件
        $("input[name='classify']").bind("click",function() {
            var classify = $('input:radio[name="classify"]:checked').val();
            if (classify == "grade1") {//类型选择
                $("#treeId").val(0);
                $("#treeName").val("");
                $("#parentId").hide();
            }else if(classify == "choice"){
                $("#treeName").val("");
                $("#parentId").show();
            }
        });


        $(function() {
            $('.summernote').summernote({
                placeholder: '请输入公告内容',
                height : 192,
                lang : 'zh-CN',
                followingToolbar: false,
                callbacks: {
                    onImageUpload: function (files) {
                        sendFile(files[0], this);
                    }
                }
            });
            var content = $("#noticeContent").val();
            $('#editor').summernote('code', content);
        });

        // 上传文件
        function sendFile(file, obj) {
            var data = new FormData();
            data.append("file", file);
            $.ajax({
                type: "POST",
                url: "/upload/image.json",
                data: data,
                cache: false,
                contentType: false,
                processData: false,
                dataType: 'json',
                success: function(result) {
                    if (result.code === 0) {
                        $(obj).summernote('editor.insertImage', result.data.src, result.data.title);
                    } else {
                        $.modal.alertError(result.msg);
                    }
                },
                error: function(error) {
                    $.modal.alertWarning("图片上传失败。");
                }
            });
        }
    </script>
</body>
</html>